#include "gui/transactionviewdetailsdialog.h"
#include "gui/separatorwidget.h"
#include "core/constants.h"

void TransactionViewDetailsDialog::initUiComponents()
{
    mainLayout = new QVBoxLayout;

    headerLabel = new QLabel;
    QFont font = headerLabel->font();
    font.setPointSize(font.pointSize() + HEADER_UPSIZE);
    headerLabel->setFont(font);
    mainLayout->addWidget(headerLabel);

    timeStampLabel = new QLabel;
    mainLayout->addWidget(timeStampLabel);
    mainLayout->addWidget(new SeparatorWidget);

    mainLayout->addStrut(32);

    deliveryStatusLabel = new QLabel;
    mainLayout->addWidget(deliveryStatusLabel);

    paidLabel = new QLabel;
    mainLayout->addWidget(paidLabel);

    slist = new ShinyList;
    mainLayout->addWidget(slist);

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    buttonBox->addButton("Edit...", QDialogButtonBox::AcceptRole);
    mainLayout->addWidget(buttonBox);

    this->setLayout(mainLayout);
}

void TransactionViewDetailsDialog::initSignalsAndSlots()
{
    connect(this->buttonBox, SIGNAL(rejected()),
            this, SLOT(reject()));
    connect(this->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
}

void TransactionViewDetailsDialog::setTransaction(TransactionModel *transaction)
{
    CustomerModel *customer = transaction->getCustomer();
    QList<StockModel *> stocks = transaction->getStocks();
    StockModel *stock;
    ShinyListItem *listItem;
    int i;

    this->headerLabel->setText(QString("Transaction with ") + customer->getName());
    this->timeStampLabel->setText(transaction->getSaleTime().toString("ddd MMMM d, yyyy"));
    this->deliveryStatusLabel->setText(QString("Delivery status: ") + transaction->getDeliveryStatus());

    if (transaction->isPaid()) {
        paidLabel->setStyleSheet("QLabel { color : green; };");
        paidLabel->setText("This transaction has been paid for.");
    } else {
        paidLabel->setStyleSheet("QLabel { color : red; };");
        paidLabel->setText("Transaction is unpaid");
    }

    this->slist->clear();
    for (i = 0; i < stocks.length(); i++) {
        stock = stocks.at(i);
        listItem = new ShinyListItem;
        listItem->setMainText(stock->getSerial());
        listItem->setSubText("Reserved");
        this->slist->addItem(listItem);
    }
}

TransactionViewDetailsDialog::TransactionViewDetailsDialog(QWidget *parent, TransactionModel *transaction) :
    QDialog(parent)
{
    this->setWindowTitle("Transaction Details");
    this->initUiComponents();
    this->initSignalsAndSlots();
    this->setTransaction(transaction);
}
